package leetcode_1_200;

import java.lang.reflect.Method;

public class LeeCode_121 {
    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
        System.out.println(maxProfit(new int[]{5,4,3,2,1}));
    }
    private static int maxProfit(int[] prices) {
        int len = prices.length;
        int[] mins = new int[len];
        int[] maxes = new int[len];
        int min = prices[0];
        for (int i = 0; i < len; i++) {
            mins[i] = Math.min(min, prices[i]);
            min = mins[i];
        }
        int max = prices[len - 1];
        for (int i = len - 1; i >= 0; i--){
            maxes[i] = Math.max(max, prices[i]);
            max = maxes[i];
        }
        int res = 0;
        for (int i = 0; i < len - 1; i++) {
            int get = maxes[i + 1] - mins[i];
            if (get > res){
                res = get;
            }
        }
        return res;
    }
}
